package com.example.iqiyitest.adapter;

import java.io.File;
import java.util.List;

import com.example.iqiyitest.R;
import com.example.iqiyitest.info.MovieDetailInfo;
import com.example.iqiyitest.utils.ImageLoader;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.v7.widget.RecyclerView.Adapter;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;



public class MyMovieAdapter extends Adapter<MyMovieAdapter.MyViewHolder> {
	private List<MovieDetailInfo> list;
	private Context context;
	private OnMenuClickListener onClickListener;
	private int itemResource;
	private ImageLoader mImageLoader;
	private int lastFocus = 0;
	
	public MyMovieAdapter(Context context, int itemResource, List<MovieDetailInfo> list, OnMenuClickListener onClickListener) {
		this.context = context;
		this.itemResource = itemResource;
		this.list = list;
		this.onClickListener = onClickListener;
		mImageLoader = ImageLoader.getInstance();// zhangzhl--后进先出法 ，优先加载 最后一个
	}
	
	@Override
	public int getItemCount() {
		return list.size();
	}

	@SuppressLint("NewApi")
	@Override
	public void onBindViewHolder(MyViewHolder arg0, final int arg1) {
//		if(arg1 == lastFocus){
//			arg0.rootView.requestFocus();
//		}
		
		arg0.rootView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Log.d("MyRecyclerView", "点击事件");
				lastFocus = arg1;
				if(onClickListener != null){
					onClickListener.onClick(arg1, v);
				} else{
				}
			}
		});
		
		mImageLoader.loadImage(
				list.get(arg1).getPosterLocalImg()
						, arg0.iconImage, true);
//		Bitmap bitmap = BitmapFactory.decodeFile(list.get(arg1).getImagePath());
//		if(bitmap != null){
//			BitmapDrawable drawble = new BitmapDrawable(bitmap);
//			arg0.iconImage.setImageDrawable(drawble);
//		}
		arg0.appName.setText(list.get(arg1).getDisplayName());
	}

	@Override
	public MyViewHolder onCreateViewHolder(ViewGroup arg0, int arg1) {
		View view = LayoutInflater.from(context).inflate(itemResource, arg0, false);
		MyViewHolder holder = new MyViewHolder(view);
		return holder;
	}
	
	class MyViewHolder extends ViewHolder{
		private View rootView;
		ImageView iconImage;
		TextView appName;
		
		public MyViewHolder(View itemView) {
			super(itemView);
			
			rootView = itemView;
			iconImage = (ImageView) rootView.findViewById(R.id.image_view);
			appName = (TextView) rootView.findViewById(R.id.name_text);
		}
		
	}
	
	public interface OnMenuClickListener {
		void onClick(int position, View v);
	}
}
